import 'dart:io';

class App {
  int port = 3000;
  var createServer;
  var handler;
  Function listen;
  Function setIp;
  Function setPort;
  Function setHandeler;
  HttpServer server;
  InternetAddress ip = InternetAddress.loopbackIPv4;

  App([handler, port]) {
    this.port = port != null ? port : this.port;
    this.handler = handler != null ? handler : this._handler;
    this.createServer = this._createServer;
    this.listen = this._listen;
    this.setIp = this._setIp;
    this.setPort = this._setPort;
    this.server = server;
    this.setHandeler = this._setHandler;
  }

  _handler(req) async { 
    req.response
      ..write('Hello World!')
      ..close(); 
  }
  
  _createServer() async {
    print('Server is running on port: ${this.port}');
    this.server = await HttpServer.bind(this.ip, this.port);
  }

  _listen() async {
    await for (HttpRequest req in this.server) {
      this._handler(req);
    }
  }

  _setIp({ InternetAddress ip }) {
    this.ip = ip;
  }

  _setPort({ int port }) {
    this.port = port;
  }

  _setHandler(handler) {
    this.handler = handler;
  }
}